﻿using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace AirportApp.Data
{
    public class Database : DataContext
    {
        public Database()
            : base("Data Source=isostore:/database.sdf")
        {
            if (!this.DatabaseExists())
            {
                CreateDatabase();

                
            }
        }

        public Table<AirportItem> Airports;
        //public Table<CountryItem> Countries;
    }

    [Table(Name="Airports")]
    public class AirportItem : INotifyPropertyChanged, INotifyPropertyChanging
    {
        private int _id;
        [Column(IsPrimaryKey = true,
            IsDbGenerated = true,
            DbType = "INT NOT NULL IDENTITY",
            CanBeNull = false,
            AutoSync = AutoSync.OnInsert)]
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        private string _code;
        [Column]
        public string Code
        {
            get { return _code; }
            set { _code = value; }
        }

        public char Key
        {
            get { return _code[0]; }
        }

        private string _name;
        [Column]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private string _country;
        [Column]
        public string Country
        {
            get { return _country; }
            set { _country = value; }
        }

        private string _countryCode;
        [Column]
        public string CountryCode
        {
            get { return _countryCode; }
            set { _countryCode = value; }
        }

        private string _countryShort;
        [Column]
        public string CountryShort
        {
            get { return _countryShort; }
            set { _countryShort = value; }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
    }
}
